import java.io.IOException;
import java.io.InputStream;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


public class ExOnScheduleTask {

	 String MaintenanceDay;
	 String MaintenanceStart;
	 String MaintenanceEnd;
	 String process;
	ExOnScheduleTask(String MaintenanceDay ,String MaintenanceStart, String MaintenanceEnd, String process) {
		MaintenanceDay = this.MaintenanceDay;
		MaintenanceStart = this.MaintenanceStart;
		MaintenanceEnd = this.MaintenanceEnd;
		process = this.process;
	}

	public int executeTask() {
		
		Calendar calender = Calendar.getInstance();

	    if(calender.get(Calendar.DAY_OF_WEEK) == Integer.parseInt(MaintenanceDay))
	    {
	    	DateFormat hhmmFormat = new SimpleDateFormat("yyyyMMddhh:mm");
			Date date = new Date();
			String yyyMMdd = hhmmFormat.format(date).substring(0, 8); 
			
			try
			{
				if(hhmmFormat.parse(yyyMMdd + MaintenanceStart).before(date) && hhmmFormat.parse(yyyMMdd + MaintenanceEnd).after(date))
				{
					try
					{
						ProcessBuilder processBuilder = new ProcessBuilder("bash", "-c", "sh " + process + ".sh");
				        processBuilder.redirectErrorStream(true); 
				        Process shellProcess = processBuilder.start();
				        
				        Worker worker = new Worker(shellProcess);
						worker.start();
						worker.join(25000);
						
						 if (worker.exit != null)
						    {
							    InputStream inputStream = shellProcess.getInputStream(); 
						        int consoleDisplay;
						        while((consoleDisplay=inputStream.read())!=-1) {
						        	System.out.println("consoleDisplay : " + consoleDisplay);
						        }
						        
					            inputStream.close();
						        
					            int i = worker.exit;
						        System.out.println("Return Type : " + i);
								return 0;
						    }
						    else
						    {
						    	worker.interrupt();
						    	return 1;
						    }
					}catch (IOException ioe) {
						System.out.println("IO Exception while execution of shell script");
						ioe.printStackTrace();
						return 1;
					}catch (InterruptedException ie) {
						System.out.println("Interrupted Exception while execution of shell script");
						ie.printStackTrace();
						return 1;
					}
				}
				else
			    {
			    	System.out.println("Maintenancew window timings are not valid.");
			    	return 1;
			    }
			}catch(ParseException pe)
			{
				System.out.println("Parsing Exception of start time and end time");
				pe.printStackTrace();
				return 1;
			}
			
	    }
	    else
	    {
	    	System.out.println("Checking for Resource File : Maintenance window is not opened.");
	    	return 1;
	    }
	}

		private static class Worker extends Thread {
			private final Process process;
			private Integer exit;
			private Worker(Process process) {
				this.process = process;
			}
			public void run() {
				try { 
					exit = process.waitFor();
				} catch (InterruptedException ignore) {
					ignore.printStackTrace();
					return;
				}
			}  
		}
		public static void main(String[] args) {
			
			ExOnScheduleTask exOnScheduleTask = new ExOnScheduleTask("SAT", "09:00", "04:00", "killProcess");
			exOnScheduleTask.executeTask();
			
			
		}
}